package com.nf.practice.fileconvert;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class JsonMergeExample {
    public static void main(String[] args) {
        String json1 = "{\n" +
                "    \"code\": 0,\n" +
                "    \"data\": {\n" +
                "        \"totalPage\": 37,\n" +
                "        \"pageSize\": 10000,\n" +
                "        \"page\": 1,\n" +
                "        \"totalCount\": 365192,\n" +
                "        \"items\": [\n" +
                "            {\n" +
                "                \"goodsNo\": \"0009100006\",\n" +
                "                \"saveDate\": \"2021-11-30\",\n" +
                "                \"billType\": \"销售\",\n" +
                "                \"costPrice\": 1.65,\n" +
                "                \"businessDate\": \"2021-11-30\",\n" +
                "                \"profileId\": 882118076943917056,\n" +
                "                \"price\": 2,\n" +
                "                \"qty\": 10,\n" +
                "                \"dpPrice\": 2,\n" +
                "                \"taxPrice\": 2,\n" +
                "                \"billNo\": \"XSD_20211130_PXQ18293971676006\",\n" +
                "                \"tradeType\": \"批发\",\n" +
                "                \"contactNo\": \"000210039700027\"\n" +
                "            }\n" +
                "        ]\n" +
                "    }\n" +
                "}";

        String json2 = "{\n" +
                "    \"code\": 0,\n" +
                "    \"data\": {\n" +
                "        \"totalPage\": 37,\n" +
                "        \"pageSize\": 10000,\n" +
                "        \"page\": 2,\n" +
                "        \"totalCount\": 365192,\n" +
                "        \"items\": [\n" +
                "            {\n" +
                "                \"goodsNo\": \"0009100006\",\n" +
                "                \"saveDate\": \"2021-11-30\",\n" +
                "                \"billType\": \"销售\",\n" +
                "                \"costPrice\": 1.65,\n" +
                "                \"businessDate\": \"2021-11-30\",\n" +
                "                \"profileId\": 882118076943917056,\n" +
                "                \"price\": 2,\n" +
                "                \"qty\": 10,\n" +
                "                \"dpPrice\": 2,\n" +
                "                \"taxPrice\": 2,\n" +
                "                \"billNo\": \"XSD_20211130_PXQ18293971676006\",\n" +
                "                \"tradeType\": \"批发\",\n" +
                "                \"contactNo\": \"000210039700027\"\n" +
                "            }\n" +
                "        ]\n" +
                "    }\n" +
                "}";

        // 解析JSON字符串为JSONObject
        JSONObject jsonObject1 = JSONObject.parseObject(json1);
        JSONObject jsonObject2 = JSONObject.parseObject(json2);

        // 获取两个JSONObject中的items数组
        JSONArray items1 = jsonObject1.getJSONObject("data").getJSONArray("items");
        JSONArray items2 = jsonObject2.getJSONObject("data").getJSONArray("items");

        // 合并两个items数组
        for (int i = 0; i < items2.size(); i++) {
            items1.add(items2.get(i));
        }

        // 将合并后的items数组放回第一个JSONObject中
        jsonObject1.getJSONObject("data").put("items", items1);

        // 更新page字段为totalPage的值
        int totalPage = jsonObject1.getJSONObject("data").getInteger("totalPage");
        jsonObject1.getJSONObject("data").put("page", totalPage);
        // 输出最终的JSONObject
        System.out.println(jsonObject1); // 2表示缩进级别
    }
}